﻿// -----------------------------------------------------------------------
// <copyright file="ConditionData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db Condition
    /// </summary>
    public class ConditionData
    {
        /// <summary>
        /// Get Condition By Id
        /// </summary>
        /// <param name="conditionsId">Param conditionsId</param>
        /// <returns>Return Condition</returns>
        public ConditionEN GetConditionById(int conditionsId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                ConditionEN conditions = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneConditionsGetById, conditionsId);
                while (reader.Read())
                {
                    conditions = new ConditionEN();

                    if (!reader.IsDBNull(0))
                    {
                        conditions.ConditionsId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        conditions.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        conditions.Name = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        conditions.Descriptions = reader.GetString(3);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return conditions;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all Condition
        /// </summary>
        /// <returns>Return list all Condition</returns>
        public IList<ConditionEN> GetConditions()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ConditionEN> lstCondition = new List<ConditionEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneConditionsGetAll);
                while (reader.Read())
                {
                    ConditionEN conditions = new ConditionEN();

                    if (!reader.IsDBNull(0))
                    {
                        conditions.ConditionsId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        conditions.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        conditions.Name = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        conditions.Descriptions = reader.GetString(3);
                    }

                    lstCondition.Add(conditions);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstCondition;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db Condition
        /// </summary>
        /// <param name="conditions">Paarm Condition</param>
        /// <returns>Insert Condition</returns>
        public int Insert(ConditionEN conditions)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Condition.Code, conditions.Code),
                                    new SqlParameter(Constants.SqlParamerterName.Condition.Name, conditions.Name),
                                    new SqlParameter(Constants.SqlParamerterName.Condition.Descriptions, conditions.Descriptions)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneConditionsInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db Condition
        /// </summary>
        /// <param name="conditions">Param Condition</param>
        public void Update(ConditionEN conditions)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Condition.ConditionsId, conditions.ConditionsId),
                                    new SqlParameter(Constants.SqlParamerterName.Condition.Code, conditions.Code),
                                    new SqlParameter(Constants.SqlParamerterName.Condition.Name, conditions.Name),
                                    new SqlParameter(Constants.SqlParamerterName.Condition.Descriptions, conditions.Descriptions)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneConditionsUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete Condition by conditionsId
        /// </summary>
        /// <param name="conditionsId">param conditionsId</param>
        public void Delete(int conditionsId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.Condition.ConditionsId, conditionsId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneConditionsDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Exist code
        /// </summary>
        /// <param name="conditionsId">Param conditionsId</param>
        /// <param name="code">Param code</param>
        /// <returns>Return Exist</returns>
        public bool Exist(int conditionsId, string code)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.Condition.ConditionsId, conditionsId),
                    new SqlParameter(Constants.SqlParamerterName.Condition.Code, code)
                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneConditionExist, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}